package com.quizzer.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.quizzer.common.utils.DateUtils;
import com.quizzer.system.convert.QuestionConverter;
import com.quizzer.system.dto.question.question.QuestionAddDto;
import com.quizzer.system.dto.question.question.QuestionOutputDto;
import com.quizzer.system.dto.question.question.QuestionQueryDto;
import com.quizzer.system.dto.question.question.QuestionUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.quizzer.system.mapper.QuestionMapper;
import com.quizzer.system.domain.Question;
import com.quizzer.system.service.IQuestionService;

/**
 * 题目Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-07
 */
@Service
public class QuestionServiceImpl implements IQuestionService
{
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询题目
     *
     * @param id 题目主键
     * @return 题目
     */
    @Override
    public QuestionOutputDto selectQuestionById(Long id)
    {
        Question question = questionMapper.selectQuestionById(id);
        return selectOne(question);
    }

    private QuestionOutputDto selectOne(Question question) {
        return QuestionConverter.toOutputDto(question);
    }

    /**
     * 查询题目列表
     *
     * @param queryDto 题目
     * @return 题目
     */
    @Override
    public List<QuestionOutputDto> selectQuestionList(QuestionQueryDto queryDto)
    {
        Question question = QuestionConverter.toEntity(queryDto);
        List<Question> questionList = questionMapper.selectQuestionList(question);
        List<QuestionOutputDto> outputDtoList = new ArrayList<>();
        for (Question item : questionList) {
            outputDtoList.add(selectOne(item));
        }
        return outputDtoList;
    }

    /**
     * 新增题目
     *
     * @param addDto 题目
     * @return 结果
     */
    @Override
    public int insertQuestion(QuestionAddDto addDto)
    {
        Question question = QuestionConverter.toEntity(addDto);
        return questionMapper.insertQuestion(question);
    }

    /**
     * 修改题目
     *
     * @param updateDto 题目
     * @return 结果
     */
    @Override
    public int updateQuestion(QuestionUpdateDto updateDto)
    {
        Question question = QuestionConverter.toEntity(updateDto);
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除题目
     *
     * @param ids 需要删除的题目主键
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(Long[] ids)
    {
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除题目信息
     *
     * @param id 题目主键
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id)
    {
        return questionMapper.deleteQuestionById(id);
    }
}
